package com.tts.dede;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.UserDictionary.Words;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Animaux extends Activity implements OnClickListener {

	// Random declaration
	private Random RANDOM = new Random();
	private MediaPlayer mPlayer = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animaux);

		Button button = (Button) findViewById(R.id.back1);
		button.setOnClickListener(this);
		((Button) findViewById(R.id.back1))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(),
								TTSActivity.class);
						startActivity(intent);
						finish();
					}
				});

		Button button1 = (Button) findViewById(R.id.New1);
		button1.setOnClickListener(this);
		((Button) findViewById(R.id.New1))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(),
								Animaux.class);
						startActivity(intent);
						finish();
					}
				});

		/*** Disparition de tout les boutons ***/
		((Button) findViewById(R.id.canard)).setVisibility(View.GONE);
		((Button) findViewById(R.id.vache)).setVisibility(View.GONE);
		((Button) findViewById(R.id.chien)).setVisibility(View.GONE);
		((Button) findViewById(R.id.chat)).setVisibility(View.GONE);
		((Button) findViewById(R.id.poule)).setVisibility(View.GONE);
		((Button) findViewById(R.id.cochon)).setVisibility(View.GONE);
		((Button) findViewById(R.id.mouton)).setVisibility(View.GONE);
		((Button) findViewById(R.id.cheval)).setVisibility(View.GONE);

		/*** Choix Random pour l'affichage d'une des images ****/
		int rand = RANDOM.nextInt(8);
		switch (rand) {
		case 0: {
			((Button) findViewById(R.id.canard)).setVisibility(View.VISIBLE);
		}
			break;
		case 1: {
			((Button) findViewById(R.id.vache)).setVisibility(View.VISIBLE);
		}
			break;
		case 2: {
			((Button) findViewById(R.id.chien)).setVisibility(View.VISIBLE);
		}
			break;
		case 3: {
			((Button) findViewById(R.id.chat)).setVisibility(View.VISIBLE);
		}
			break;
		case 4: {
			((Button) findViewById(R.id.poule)).setVisibility(View.VISIBLE);
		}
			break;
		case 5: {
			((Button) findViewById(R.id.cochon)).setVisibility(View.VISIBLE);
		}
			break;
		case 6: {
			((Button) findViewById(R.id.mouton)).setVisibility(View.VISIBLE);
		}
			break;
		case 7: {
			((Button) findViewById(R.id.cheval)).setVisibility(View.VISIBLE);
		}
			break;
		}
		;

		/****** Ici les actions sur les bouton *****/
		((Button) findViewById(R.id.canard))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Canard";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.coincoin);
					}
				});
		((Button) findViewById(R.id.vache))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Vache";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.vache);
					}
				});
		((Button) findViewById(R.id.chien))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Chien";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.chien);
					}
				});
		((Button) findViewById(R.id.chat))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Chat";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.chat);
					}
				});
		((Button) findViewById(R.id.poule))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Poule";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.poule);
					}
				});
		((Button) findViewById(R.id.cochon))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Cochon";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.pig);
					}
				});
		((Button) findViewById(R.id.mouton))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Mouton";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.sheep);
					}
				});
		((Button) findViewById(R.id.cheval))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						String words = "Cheval";
						Toast.makeText(getApplicationContext(),
								words.toUpperCase(), Toast.LENGTH_LONG).show();
						playSound(R.raw.horse);
					}
				});

		/********* Fin du OnCreate *************/
	}

	@Override
	public void onPause() {
		super.onPause();
		if (mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();
		}
	}

	private void playSound(int resId) {
		if (mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();
		}
		mPlayer = MediaPlayer.create(this, resId);
		mPlayer.start();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}
